﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XSTDZ.ERP.Domain.Models
{
    /// <summary>
    /// 组织机构
    /// </summary>
    public partial class Org
    {
        public Org()
        {
            this.Depts = new HashSet<Dept>();
            this.Users = new HashSet<User>();
            this.Orgs = new HashSet<Org>();
            this.Storehouses = new HashSet<Storehouse>();
        }
        /// <summary>
        /// 当OrgParentId为空时，自动添加OrgParentId为当前ID
        /// </summary>
        /// <param name="org"></param>
        /// <returns></returns>
        public Org AddOrgParentId(Org org)
        {

            if (Guid.Equals(org.ParentID, Guid.Empty))
            {
                org.ParentID = org.Id;
                org.Level = 1;
            }
            else
            {
                org.ParentID = org.Parent.Id;
                org.Level =Convert.ToSByte(org.Parent.Level+1);
            }
            return this;
        }
     
       /// <summary>
       /// 添加下级组织机构
       /// </summary>
       /// <param name="childOrg">下级组织机构结点</param>
       /// <returns>组织机构</returns>
       public Org AddChildOrg(Org childOrg)
       {
           if (!this.Orgs.Contains(childOrg))
           {
               childOrg.Parent = this;
               this.Orgs.Add(childOrg);
           }
           return this;
       }
        /*
       /// <summary>
       /// 重置父级组织机构
       /// </summary>
       /// <param name="parentOrg">父级组织机构结点</param>
       /// <returns>组织机构</returns>
       public Org ResetParentOrg(Org parentOrg)
       {
           this.Parent.Orgs.Remove(this);
           this.Parent = parentOrg;
           return parentOrg.AddChildOrg(this);            
       }
       /// <summary>
       /// 添加下级部门
       /// </summary>
       /// <param name="dept">部门</param>
       /// <returns>组织机构</returns>
       public Org AddChildDept(Dept dept)
       {
           if (!this.Depts.Contains(dept))
           {
               dept.Org = this;
               this.Depts.Add(dept);
           }
           return this;
       }
       */
    }
}
